package convert

import (
	"log/slog"
	"statsphere/pb"
	"statsphere/pkg/gwprop"
	"statsphere/pkg/snowflake"
	"strings"
	"time"
)

func ConvertToRuleCtrlLog(ctrlLog *pb.CtrlLog) *pb.RuleCtrlLog {
	a := new(pb.RuleCtrlLog)
	a.Time = uint64(time.Now().UnixMilli())
	a.RequestId = snowflake.GetId()
	a.Mac = gwprop.GetProp().MAC
	a.EpId = gwprop.GetProp().EPID
	a.GwId = gwprop.GetProp().GWID
	//
	a.LogType = "rule_engine_stat"

	batchIdSplitArray := strings.Split(ctrlLog.BatchId, "_")
	if len(batchIdSplitArray) != 2 {
		slog.Error("batchIdSplitArray_len_error")
	}
	_ruleId := batchIdSplitArray[0]
	//
	ctrlResult := &pb.CtrlResult{
		State:    pb.StateType_suc,
		RuleId:   _ruleId,
		BatchId:  ctrlLog.BatchId,
		ErrItems: ctrlLog.Items,
	}
	if len(ctrlLog.Items) > 0 {
		ctrlResult.State = pb.StateType_err
	}
	a.Data = ctrlResult
	return a
}
